package tools.blat;

import java.io.*;


public class mPslParser2 {
	
	public static void pslParser(String inFile, String outFile) throws Exception{
		try {
			BufferedReader reader = new BufferedReader(new FileReader(inFile));
			PslParser parser = new PslParser(reader);
			BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
			
			//Writing psl header info
			bw.write("psLayout version 3\n");
			bw.write("\n");
			bw.write("match     mis-    rep.    N's     Q gap   Q gap   T gap   T gap   strand  Q               Q       Q       Q       T               T       T       T       block   blockSizes      qStarts  tStarts\n");
			bw.write("          match   match           count   bases   count   bases           name            size    start   end     name            size    start   end     count\n");
			bw.write("---------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
			
			
			EstAlignment best = null;
			
			while(parser.hasNext()) {
				EstAlignment tmp = (EstAlignment)parser.next();
				
				if(best == null){ // If first, just add
					best = tmp;
					continue;
				}
				
				if (!best.qname.equalsIgnoreCase(tmp.qname)) { // If new gene name, add best to vector, let the first element be best
					//SKRIV UT BEST I FILEN
//					boolean prot=false;
					bw.write(best.matches+"\t");
					bw.write(best.mismatches+"\t");
					bw.write(best.repmatches+"\t");
					bw.write(best.ncount+"\t");
					bw.write(best.qnuminsert+"\t");
					bw.write(best.qbaseinsert+"\t");
					bw.write(best.tnuminsert+"\t");
					bw.write(best.tbaseinsert+"\t");
					bw.write(best.strand[0]+"\t");
					bw.write(best.qname+"\t");
					
					bw.write(best.qsize+"\t");
					bw.write(best.qstart+"\t");
					bw.write(best.qend+"\t");
					bw.write(best.tname+"\t");
					bw.write(best.tsize+"\t");
					bw.write(best.tstart+"\t");
					bw.write(best.tend+"\t");
					bw.write((best.blockSizes).length+"\t");
					
					for(int j=0; j<(best.blockSizes).length;j++)
						bw.write(best.blockSizes[j]+",");
					bw.write("\t");
					for(int j=0; j<(best.blockSizes).length;j++)
						bw.write(best.qStarts[j]+",");
					bw.write("\t");
					for(int j=0; j<(best.blockSizes).length;j++)
						bw.write(best.tStarts[j]+",");
					bw.write("\n");
					
					best = tmp;
				} else if(tmp.getIdentity() > best.getIdentity()) // If same name as previous, save the gene with best score
					best = tmp;
			}
			//SKRIV UT BEST IGEN
//			boolean prot=false;
			bw.write(best.matches+"\t");
			bw.write(best.mismatches+"\t");
			bw.write(best.repmatches+"\t");
			bw.write(best.ncount+"\t");
			bw.write(best.qnuminsert+"\t");
			bw.write(best.qbaseinsert+"\t");
			bw.write(best.tnuminsert+"\t");
			bw.write(best.tbaseinsert+"\t");
			bw.write(best.strand[0]+"\t");
			bw.write(best.qname+"\t");
			bw.write(best.qsize+"\t");
			bw.write(best.qstart+"\t");
			bw.write(best.qend+"\t");
			bw.write(best.tname+"\t");
			bw.write(best.tsize+"\t");
			bw.write(best.tstart+"\t");
			bw.write(best.tend+"\t");
			bw.write((best.blockSizes).length+"\t");
			for(int j=0; j<(best.blockSizes).length;j++)
				bw.write(best.blockSizes[j]+",");
			bw.write("\t");
			for(int j=0; j<(best.blockSizes).length;j++)
				bw.write(best.qStarts[j]+",");
			bw.write("\t");
			for(int j=0; j<(best.blockSizes).length;j++)
				bw.write(best.tStarts[j]+",");
			bw.write("\n");
			
			bw.close();
			
		} catch(FileNotFoundException fnfe){
			System.out.println("Error reading file: "+fnfe);
			fnfe.printStackTrace();
		} catch(Exception e){
			System.out.println("Error : "+e);
			e.printStackTrace();
		} 
	}
	
	
	public static void main(String args[]) {
		
		if(args.length != 1){
			System.out.println("Wrong number of parameters!" + '\n' + "Usage: mPslParser path/Infile");
			System.exit(0);
			
		}
		
		String inFile = args[0];
		
		String outFile = inFile.substring(0,(inFile.length()-4))+"_parsed"+inFile.substring(inFile.length()-4, inFile.length());
		
		
		
		long bParse = ((System.currentTimeMillis())/1000);
		System.out.println("--- PARSING ---");
		try {
			pslParser( inFile , outFile);
		} catch(Exception e) {
			System.out.println(e);
		}
		long aParse = ((System.currentTimeMillis())/1000);
		System.out.println("Done in "+(aParse-bParse)+" seconds.");
		System.out.println("--- DONE ---");
		
		
		
	}
	
	
}
